<?php 
class News extends CI_Controller  {
	function __construct()
 {
  	parent::__construct();
  	$this->load->helper('url');
    //$this->load->database();
    $this->load->model('TB_News');
	$this->load->model('TB_Menu');

 }
	function index()
	{
		$data["MenuEntity"]=	$this->TB_Menu->get_Menu_OneRow($_GET["ClassID"],$_GET["TypeID"]);
	    $data['results'] = $this->TB_News->get_NewsByType($_GET["ClassID"],$_GET["TypeID"]);
	    $this->load->view('view_news', $data);

	//		$this->load->view("Add_News");
	//	$data["AllNew"]=$this->TB_News->get_News();
	//	$this->load->view("view_news",$data);
	}
	function insert()
	{
		$data=$this->do_upload();
		$ImgPath=$data['upload_data']['file_ext']==''?'':$data['upload_data']['file_name'];
		$MenuEntity=$this->TB_Menu->get_Menu_OneRow($_POST["menu_class"],$_POST["menu_type"]);
		$NewsEnt=array("Title"=>$_POST["Title"],
		"Content"=>$_POST["Content"],
		"ClassID"=>$MenuEntity[0]->ClassID,
		"ClassName"=>$MenuEntity[0]->ClassName,
		"TypeID"=>$MenuEntity[0]->TypeID,
		"TypeName"=>$MenuEntity[0]->TypeName,
		"ImgPath"=>$ImgPath,
		"Hits"=>0,
		"NewsSource"=>$_POST["NewsSource"],
		"Addtime"=>date("Y-m-d"),
		"ShowinHome"=>0,
		"OrderNum"=>$_POST["OrderNum"]);
		$this->TB_News->insert($NewsEnt); 
		$MaxNewsID=$this->TB_News->get_MaxNewsID();
	//	print_r($MaxNewsID);
		$this->Refresh($MaxNewsID[0]->NewsID);
	}
	function do_upload()
	 {
	 	 //	$data=$this->do_upload();
		  $config['upload_path'] = getcwd().'./uploads/';
		  $config['allowed_types'] = 'jpg|jpeg|gif|png';
		  $config['max_size'] = '1000';
		  $config['max_width']  = '1024';
		  $config['max_height']  = '768';
		  $config['overwrite']=true;
 	   	  $config['file_name']=date('ymdhis');

		  $this->load->library('upload', $config);
		  $this->upload->initialize($config);
		  $this->upload->do_upload();
		  return  $data = array('upload_data' => $this->upload->data());

	 }
	public function update()
	{
	//	"ImgPath"=>$_POST["ImgPath"],
	//	"Addtime"=>date("Y-m-d"),
	//	"ShowinHome"=>$_POST["ShowinHome"],
	//	"Hits"=>$_POST["Hits"],
		$data=$this->do_upload();
		$imgpath=$data['upload_data']['file_ext']==''?'':$data['upload_data']['file_name'];
		if($imgpath=="")
		{
			$NewsEntity=$this->TB_News->get_NewsByClassID($_POST["NewsID"]);
			$imgpath=$NewsEntity[0]->ImgPath;
		}
		$MenuEntity=$this->TB_Menu->get_Menu_OneRow($_POST["menu_class"],$_POST["menu_type"]);
		$NewsEnt=array("NewsID"=>$_POST["NewsID"], 
		"Title"=>$_POST["Title"],
		"Content"=>$_POST["Content"],
		"ClassName"=>$MenuEntity[0]->ClassName,
		"TypeName"=>$MenuEntity[0]->TypeName,
		"ClassID"=>$MenuEntity[0]->ClassID, 
		"TypeID"=>$MenuEntity[0]->TypeID,
		"NewsSource"=>$_POST["NewsSource"],
		"ImgPath"=>	$imgpath,
		"OrderNum"=>$_POST["OrderNum"]);
		$this->TB_News->update($NewsEnt);
		$this->Refresh($_POST["NewsID"]);
	}

	public function Delete()
	{	
		$NewsEntity=$this->TB_News->get_NewsByNewsID($_GET["NewsID"]);
		$this->TB_News->Delete($_GET["NewsID"]); 
		$this->RefreshByType($NewsEntity[0]->ClassID,$NewsEntity[0]->TypeID);
	}
	public function UpdateImgPath()
	{
		$this->TB_News->UpdateImgPath($_Get["ImgPath"],$_Get["NewsID"]);
	}
	public function UpdateOrderNum()
	{
		$this->TB_News->UpdateImgPath($_Get["OrderNum"],$_Get["NewsID"]);
	}
	public function get_News()
	{
		return	$this->TB_News->get_News();
	}
	function get_News_TypeName($ClassName,$TypeName)
	{
		return	$this->TB_News->get_News_TypeName($ClassName,$TypeName);
	}
	function Refresh($NewsID)
	{
		$NewsEntity=$this->TB_News->get_NewsByNewsID($NewsID);
		$data["MenuEntity"]=	$this->TB_Menu->get_Menu_OneRow($NewsEntity[0]->ClassID,$NewsEntity[0]->TypeID);
	    $data['results'] = $this->TB_News->get_NewsByType($NewsEntity[0]->ClassID,$NewsEntity[0]->TypeID);
	    $this->load->view('view_news', $data);
	}
	function RefreshByType($ClassID,$TypeID)
	{
		
		$data["MenuEntity"]=	$this->TB_Menu->get_Menu_OneRow($ClassID,$TypeID);
	    $data['results'] = $this->TB_News->get_NewsByType($ClassID,$TypeID);
	    $this->load->view('view_news', $data);
	}
} 